import router from "@/router";
import { reject } from "lodash";
import { resolve } from "path";
import { defineAsyncComponent } from "vue";
import { RouteRecordRaw } from 'vue-router'
function isURL(s:string) {
    return /^http[s]?:\/\/.*/.test(s)
}

export const formatRoutes = (menu: Array<object>, first: boolean) => {
    const menuArr: object[] = []
    //引入所有views下.vue文件
    const modules = import.meta.glob("../views/**/**.vue")
    // console.log(modules);
    
    // 在这里不做对地址以及 参数的验证规则
    // 当为[]时 返回
    // console.log(menu);
    
    if (menu.length == 0) return
    // 获取当前的所有数组实例
    let originalRouter = router.getRoutes()
    // console.log(originalRouter);
    let routerList: string[] = originalRouter.map(item => item.path)
    // console.log(routerList);

    // 对获取到的数据进行循环
    menu.forEach((item: any) => {
        // 将当前数组与已有数组进行比对 如果有则返回
        if (routerList.includes(item.path)) return
        const isChild = item.children.length !== 0
        const path = (() => {
            // 此为获得的路由地址
            if (!item.path) {
                // 如果没有返回
                return
            } else if (first) {
                // 当他为 first 也就是true 的时候 返回替换过的路径，将index替换为''
                return item.path.replace('/index', '')
            } else {
                // 如果为false 的时候 直接返回路由地址
                return item.path
            }
        })()    
        // console.log(isChild,!first,'first',path);
        
        let aRouter:RouteRecordRaw = {
            path,
            name: item.label,
            // component: () =>defineAsyncComponent(() => import(`../views${item.path}.vue`)) ,
            component:(()=>new Promise((resolve,reject)=>{
                
                if (first) {
                    // console.log(1);
                    resolve(import('../views/home/index.vue')as any)
                    // 判断是否为多层路由
                  } else if (isChild && !first) {
                    // console.log(2);
                    resolve(import('../page/layout.vue') as any )
                    // 判断是否为最终的页面视图
                  } else {
                    // console.log(3);
                    resolve(defineAsyncComponent(modules[/* @vite-ignore */ `../views${item.path}.vue`]as any) )
                  }
            })) ,
            redirect: (() => {
                // console.log(123,path);
                if (!isChild && first && !isURL(path)) return `${path}/index`
                else return ''
            })(),
            children: !isChild ? (():any => {
                if (first) {
                    if (!isURL(path)) item.path = `${path}/index`
                    return [{
                        component: () =>defineAsyncComponent(() => import( /* @vite-ignore */ `../views/${item.path}.vue`)) ,
                        // icon: icon,
                        name: item.label,
                        // meta: meta,
                        path: 'index'
                    }]
                }
                return []
            })() : (() => {
                return formatRoutes(item.children, false)
            })()
        }
        menuArr.push(aRouter)
        
    })
    return menuArr
}